package smu.KYBank.test;

import smu.KYBank.DBHandler;
import smu.KYBank.KYBwithdraw;
import android.database.Cursor;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

public class KYBwithdrawTest2 extends
		ActivityInstrumentationTestCase2<KYBwithdraw> {
	private KYBwithdraw mActivity;
	EditText w_money;
	TextView bal;
	String login_id = "rul", balance;

	public KYBwithdrawTest2() {
		super("smu.KYBank", KYBwithdraw.class);
	}
		
	protected void setUp() throws Exception {
		super.setUp();
		
		mActivity = this.getActivity();
		
		bal = (TextView) mActivity.findViewById(smu.KYBank.R.id.w_bal);

		w_money = (EditText) mActivity.findViewById(smu.KYBank.R.id.w_money);
		try {
			runTestOnUiThread(new Runnable() {
				
				public void run() {
					// TODO Auto-generated method stub
					DBHandler dbhandler = DBHandler.open(mActivity);
					Cursor cursor = dbhandler.select_id(login_id + "	");
					if (cursor.moveToFirst()) {
						do {
							balance = cursor.getString(cursor.getColumnIndex("balance"));
							bal = (TextView) mActivity.findViewById(smu.KYBank.R.id.w_bal);
							bal.setText(balance);
						} while (cursor.moveToNext());
					}
					dbhandler.close();
				}
			});
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void testWithdraw() {
		// case : rul's balance was 10450 -> can't withdraw 20000won. -> return -2.
		TouchUtils.tapView(this, w_money);
		sendKeys(KeyEvent.KEYCODE_2, KeyEvent.KEYCODE_0, KeyEvent.KEYCODE_0, KeyEvent.KEYCODE_0, KeyEvent.KEYCODE_0);

		assertEquals(-2, mActivity.withdraw(login_id));
	}
}
